{
    class RokerMediator extends Laya.EventDispatcher {
        constructor() {
            super();
            this._touchId = null;
            this.initPos = [];
            this.rect = null;
            this.direct = [0, 0];
            this.speed = 0;
            this.angle = 0;
        }

        destoryEvent(){
            Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMove);
            Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.stop);
            Laya.stage.off(Laya.Event.MOUSE_UP, this, this.stop);
        }

        /**
         * 虚拟摇杆初始化
         * @param {*} skin 摇杆ui
         * @param {*} rokerType 0-固定位置 1-放手自动归位 2-放手自动隐藏
         * @param {*} isDrag 到底峰值后是否可拖拽， 固定位置的摇杆一定不可拖拽
         */
        init(skin, rokerType, isDrag = false) {
            this._skin = skin;
            this._rokerType = rokerType;
            this._isDrag = isDrag;
            this.active = false;

            if (this._rokerType == 2) {
                this._skin.visible = false;
            }
            // Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.start);
        }

        //设置摇杆初始化位置
        setInitPos() {
            this.initPos = [this._skin.x, this._skin.y];
        }

        start(e) {
            this.active = true;
            this._touchId = e.touchId;
            if (this._rokerType != 0) {
                this._skin.pos(Laya.stage.mouseX, Laya.stage.mouseY);
            } else {
                this.onMove(e);
            }
            Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onMove);
            Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.stop, [false]);
            Laya.stage.on(Laya.Event.MOUSE_UP, this, this.stop, [true]);
        }

        /**
         * 
         * @param {*} isExecute 是否执行
         * @param {*} e 
         */
        stop(isExecute, e) {
            if (e.touchId != this._touchId) {
                return;
            }
            if (!isExecute) {
                this.speed = 0;
            }
            this.event('roker_execute');
            //重置摇杆
            this.active = false;
            this.isMove = false;
            this.angle = 0;
            this.speed = 0;
            this.direct = [0, 0];
            this._skin.point.pos(this._skin.width / 2, this._skin.height / 2);
            Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMove);
            Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.stop);
            Laya.stage.off(Laya.Event.MOUSE_UP, this, this.stop);
            if (this._rokerType == 1) {
                this._skin.pos(this.initPos[0], this.initPos[1]);
            } else if (this._rokerType == 2) {
                this._skin.visible = false;
            }
        }

        //取消摇杆操作
        cancle(e) {
            if (e.touchId != this._touchId) {
                return;
            }
            console.log('cancle');
            this.stop(false, e);
        }

        onMove(e) {
            if (e.touchId != this._touchId) {
                return;
            }
            let r = this._skin.width / 2;
            let disX = this._skin.mouseX - r;
            let disY = this._skin.mouseY - r;
            let dis = Math.sqrt(Math.pow(disX, 2) + Math.pow(disY, 2));

            if (this.rokerType != 0 && this._isDrag) {
                //超过半径的位移比例
                let selfMoveN = dis / r - 1;
                if (selfMoveN > 0) {
                    let startX = this._skin.x + selfMoveN * disX;
                    let startY = this._skin.y + selfMoveN * disY;
                    this._skin.pos(startX, startY);
                }
            }
            //超过半径的时候取半径
            let n = Math.min(r / dis, 1);
            let changeX = n * disX;
            let changeY = n * disY;
            this._skin.point.x = this._skin.width / 2 + changeX;
            this._skin.point.y = this._skin.height / 2 + changeY;

            this.direct = [disX, disY];
            this.speed = Math.min(dis / r, 1);
            this.angle = Math.atan2(disY, disX) * 180 / Math.PI;
            this.event('roker_move');
            // this._skin.arrow.rotation = this.angle + 90;
        }
    }

    Sail.class(RokerMediator, "Control.Room.RokerMediator");
}